<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.13.2"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>CAD_Doc: Googletest Primer</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script type="text/javascript" src="clipboard.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="cookie.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
  $(function() { init_search(); });
/* @license-end */
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectalign">
   <div id="projectname">CAD_Doc<span id="projectnumber">&#160;1.0</span>
   </div>
   <div id="projectbrief">cad synopsis</div>
  </td>
    <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <span id="MSearchSelect"                onmouseover="return searchBox.OnSearchSelectShow()"                onmouseout="return searchBox.OnSearchSelectHide()">&#160;</span>
          <input type="text" id="MSearchField" value="" placeholder="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.13.2 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search/",'.html');
/* @license-end */
</script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() { codefold.init(0); });
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function(){initNavTree('md__build_2__deps_2googletest-src_2docs_2primer.html',''); initResizable(true); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>

<div><div class="header">
  <div class="headertitle"><div class="title">Googletest Primer</div></div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><a class="anchor" id="autotoc_md224"></a></p>
<h1><a class="anchor" id="autotoc_md225"></a>
Introduction: Why googletest?</h1>
<p><em>googletest</em> helps you write better C++ tests.</p>
<p>googletest is a testing framework developed by the Testing Technology team with Google's specific requirements and constraints in mind. Whether you work on Linux, Windows, or a Mac, if you write C++ code, googletest can help you. And it supports <em>any</em> kind of tests, not just unit tests.</p>
<p>So what makes a good test, and how does googletest fit in? We believe:</p>
<ol type="1">
<li>Tests should be <em>independent</em> and <em>repeatable</em>. It's a pain to debug a test that succeeds or fails as a result of other tests. googletest isolates the tests by running each of them on a different object. When a test fails, googletest allows you to run it in isolation for quick debugging.</li>
<li>Tests should be well <em>organized</em> and reflect the structure of the tested code. googletest groups related tests into test suites that can share data and subroutines. This common pattern is easy to recognize and makes tests easy to maintain. Such consistency is especially helpful when people switch projects and start to work on a new code base.</li>
<li>Tests should be <em>portable</em> and <em>reusable</em>. Google has a lot of code that is platform-neutral; its tests should also be platform-neutral. googletest works on different OSes, with different compilers, with or without exceptions, so googletest tests can work with a variety of configurations.</li>
<li>When tests fail, they should provide as much <em>information</em> about the problem as possible. googletest doesn't stop at the first test failure. Instead, it only stops the current test and continues with the next. You can also set up tests that report non-fatal failures after which the current test continues. Thus, you can detect and fix multiple bugs in a single run-edit-compile cycle.</li>
<li>The testing framework should liberate test writers from housekeeping chores and let them focus on the test <em>content</em>. googletest automatically keeps track of all tests defined, and doesn't require the user to enumerate them in order to run them.</li>
<li>Tests should be <em>fast</em>. With googletest, you can reuse shared resources across tests and pay for the set-up/tear-down only once, without making tests depend on each other.</li>
</ol>
<p>Since googletest is based on the popular xUnit architecture, you'll feel right at home if you've used JUnit or PyUnit before. If not, it will take you about 10 minutes to learn the basics and get started. So let's go!</p>
<h1><a class="anchor" id="autotoc_md226"></a>
Beware of the nomenclature</h1>
<p>{: .callout .note} <em>Note:</em> There might be some confusion arising from different definitions of the terms <em>Test</em>, <em>Test Case</em> and <em>Test Suite</em>, so beware of misunderstanding these.</p>
<p>Historically, googletest started to use the term <em>Test Case</em> for grouping related tests, whereas current publications, including International Software Testing Qualifications Board (<a href="http://www.istqb.org/">ISTQB</a>) materials and various textbooks on software quality, use the term <em><a href="http://glossary.istqb.org/en/search/test%20suite">Test Suite</a></em> for this.</p>
<p>The related term <em>Test</em>, as it is used in googletest, corresponds to the term <em><a href="http://glossary.istqb.org/en/search/test%20case">Test Case</a></em> of ISTQB and others.</p>
<p>The term <em>Test</em> is commonly of broad enough sense, including ISTQB's definition of <em>Test Case</em>, so it's not much of a problem here. But the term <em>Test Case</em> as was used in Google Test is of contradictory sense and thus confusing.</p>
<p>googletest recently started replacing the term <em>Test Case</em> with <em>Test Suite</em>. The preferred API is <em><a class="el" href="class_test_suite.html">TestSuite</a></em>. The older TestCase API is being slowly deprecated and refactored away.</p>
<p>So please be aware of the different definitions of the terms:</p>
<table class="markdownTable">
<tr class="markdownTableHead">
<th class="markdownTableHeadLeft">Meaning   </th><th class="markdownTableHeadLeft">googletest Term   </th><th class="markdownTableHeadLeft"><a href="http://www.istqb.org/">ISTQB</a> Term    </th></tr>
<tr class="markdownTableRowOdd">
<td class="markdownTableBodyLeft">Exercise a particular program path with specific input values and verify the results   </td><td class="markdownTableBodyLeft">TEST()   </td><td class="markdownTableBodyLeft"><a href="http://glossary.istqb.org/en/search/test%20case">Test Case</a>   </td></tr>
</table>
<h1><a class="anchor" id="autotoc_md227"></a>
Basic Concepts</h1>
<p>When using googletest, you start by writing <em>assertions</em>, which are statements that check whether a condition is true. An assertion's result can be <em>success</em>, <em>nonfatal failure</em>, or <em>fatal failure</em>. If a fatal failure occurs, it aborts the current function; otherwise the program continues normally.</p>
<p><em>Tests</em> use assertions to verify the tested code's behavior. If a test crashes or has a failed assertion, then it <em>fails</em>; otherwise it <em>succeeds</em>.</p>
<p>A <em>test suite</em> contains one or many tests. You should group your tests into test suites that reflect the structure of the tested code. When multiple tests in a test suite need to share common objects and subroutines, you can put them into a <em>test fixture</em> class.</p>
<p>A <em>test program</em> can contain multiple test suites.</p>
<p>We'll now explain how to write a test program, starting at the individual assertion level and building up to tests and test suites.</p>
<h1><a class="anchor" id="autotoc_md228"></a>
Assertions</h1>
<p>googletest assertions are macros that resemble function calls. You test a class or function by making assertions about its behavior. When an assertion fails, googletest prints the assertion's source file and line number location, along with a failure message. You may also supply a custom failure message which will be appended to googletest's message.</p>
<p>The assertions come in pairs that test the same thing but have different effects on the current function. <code>ASSERT_*</code> versions generate fatal failures when they fail, and <b>abort the current function</b>. <code>EXPECT_*</code> versions generate nonfatal failures, which don't abort the current function. Usually <code>EXPECT_*</code> are preferred, as they allow more than one failure to be reported in a test. However, you should use <code>ASSERT_*</code> if it doesn't make sense to continue when the assertion in question fails.</p>
<p>Since a failed <code>ASSERT_*</code> returns from the current function immediately, possibly skipping clean-up code that comes after it, it may cause a space leak. Depending on the nature of the leak, it may or may not be worth fixing - so keep this in mind if you get a heap checker error in addition to assertion errors.</p>
<p>To provide a custom failure message, simply stream it into the macro using the <code>&lt;&lt;</code> operator or a sequence of such operators. See the following example, using the <a href="reference/assertions.md#EXPECT_EQ"><code>ASSERT_EQ</code> and <code>EXPECT_EQ</code></a> macros to verify value equality:</p>
<div class="fragment"><div class="line">ASSERT_EQ(x.size(), y.size()) &lt;&lt; <span class="stringliteral">&quot;Vectors x and y are of unequal length&quot;</span>;</div>
<div class="line"> </div>
<div class="line"><span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; x.size(); ++i) {</div>
<div class="line">  EXPECT_EQ(x[i], y[i]) &lt;&lt; <span class="stringliteral">&quot;Vectors x and y differ at index &quot;</span> &lt;&lt; i;</div>
<div class="line">}</div>
</div><!-- fragment --><p>Anything that can be streamed to an <code>ostream</code> can be streamed to an assertion macro&ndash;in particular, C strings and <code>string</code> objects. If a wide string (<code>wchar_t*</code>, <code>TCHAR*</code> in <code>UNICODE</code> mode on Windows, or <code>std::wstring</code>) is streamed to an assertion, it will be translated to UTF-8 when printed.</p>
<p>GoogleTest provides a collection of assertions for verifying the behavior of your code in various ways. You can check Boolean conditions, compare values based on relational operators, verify string values, floating-point values, and much more. There are even assertions that enable you to verify more complex states by providing custom predicates. For the complete list of assertions provided by GoogleTest, see the <a class="el" href="md__build_2__deps_2googletest-src_2docs_2reference_2assertions.html">Assertions Reference</a>.</p>
<h1><a class="anchor" id="autotoc_md229"></a>
Simple Tests</h1>
<p>To create a test:</p>
<ol type="1">
<li>Use the <code>TEST()</code> macro to define and name a test function. These are ordinary C++ functions that don't return a value.</li>
<li>In this function, along with any valid C++ statements you want to include, use the various googletest assertions to check values.</li>
<li>The test's result is determined by the assertions; if any assertion in the test fails (either fatally or non-fatally), or if the test crashes, the entire test fails. Otherwise, it succeeds.</li>
</ol>
<div class="fragment"><div class="line">TEST(TestSuiteName, TestName) {</div>
<div class="line">  ... test body ...</div>
<div class="line">}</div>
</div><!-- fragment --><p><code>TEST()</code> arguments go from general to specific. The <em>first</em> argument is the name of the test suite, and the <em>second</em> argument is the test's name within the test suite. Both names must be valid C++ identifiers, and they should not contain any underscores (<code>_</code>). A test's <em>full name</em> consists of its containing test suite and its individual name. Tests from different test suites can have the same individual name.</p>
<p>For example, let's take a simple integer function:</p>
<div class="fragment"><div class="line"><span class="keywordtype">int</span> Factorial(<span class="keywordtype">int</span> n);  <span class="comment">// Returns the factorial of n</span></div>
</div><!-- fragment --><p>A test suite for this function might look like:</p>
<div class="fragment"><div class="line"><span class="comment">// Tests factorial of 0.</span></div>
<div class="line">TEST(FactorialTest, HandlesZeroInput) {</div>
<div class="line">  EXPECT_EQ(Factorial(0), 1);</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Tests factorial of positive numbers.</span></div>
<div class="line">TEST(FactorialTest, HandlesPositiveInput) {</div>
<div class="line">  EXPECT_EQ(Factorial(1), 1);</div>
<div class="line">  EXPECT_EQ(Factorial(2), 2);</div>
<div class="line">  EXPECT_EQ(Factorial(3), 6);</div>
<div class="line">  EXPECT_EQ(Factorial(8), 40320);</div>
<div class="line">}</div>
</div><!-- fragment --><p>googletest groups the test results by test suites, so logically related tests should be in the same test suite; in other words, the first argument to their <code>TEST()</code> should be the same. In the above example, we have two tests, <code>HandlesZeroInput</code> and <code>HandlesPositiveInput</code>, that belong to the same test suite <code>FactorialTest</code>.</p>
<p>When naming your test suites and tests, you should follow the same convention as for <a href="https://google.github.io/styleguide/cppguide.html#Function_Names">naming functions and classes</a>.</p>
<p><b>Availability</b>: Linux, Windows, Mac.</p>
<h1><a class="anchor" id="same-data-multiple-tests"></a>
Test Fixtures: Using the Same Data Configuration for Multiple Tests</h1>
<p>If you find yourself writing two or more tests that operate on similar data, you can use a <em>test fixture</em>. This allows you to reuse the same configuration of objects for several different tests.</p>
<p>To create a fixture:</p>
<ol type="1">
<li>Derive a class from <code><a class="el" href="classtesting_1_1_test.html">testing::Test</a></code> . Start its body with <code>protected:</code>, as we'll want to access fixture members from sub-classes.</li>
<li>Inside the class, declare any objects you plan to use.</li>
<li>If necessary, write a default constructor or <code>SetUp()</code> function to prepare the objects for each test. A common mistake is to spell <code>SetUp()</code> as <b><code>Setup()</code></b> with a small <code>u</code> - Use <code>override</code> in C++11 to make sure you spelled it correctly.</li>
<li>If necessary, write a destructor or <code>TearDown()</code> function to release any resources you allocated in <code>SetUp()</code> . To learn when you should use the constructor/destructor and when you should use <code>SetUp()/TearDown()</code>, read the <a href="faq.md#CtorVsSetUp">FAQ</a>.</li>
<li>If needed, define subroutines for your tests to share.</li>
</ol>
<p>When using a fixture, use <code>TEST_F()</code> instead of <code>TEST()</code> as it allows you to access objects and subroutines in the test fixture:</p>
<div class="fragment"><div class="line">TEST_F(TestFixtureName, TestName) {</div>
<div class="line">  ... test body ...</div>
<div class="line">}</div>
</div><!-- fragment --><p>Like <code>TEST()</code>, the first argument is the test suite name, but for <code>TEST_F()</code> this must be the name of the test fixture class. You've probably guessed: <code>_F</code> is for fixture.</p>
<p>Unfortunately, the C++ macro system does not allow us to create a single macro that can handle both types of tests. Using the wrong macro causes a compiler error.</p>
<p>Also, you must first define a test fixture class before using it in a <code>TEST_F()</code>, or you'll get the compiler error "`virtual outside class
declaration`".</p>
<p>For each test defined with <code>TEST_F()</code>, googletest will create a <em>fresh</em> test fixture at runtime, immediately initialize it via <code>SetUp()</code>, run the test, clean up by calling <code>TearDown()</code>, and then delete the test fixture. Note that different tests in the same test suite have different test fixture objects, and googletest always deletes a test fixture before it creates the next one. googletest does <b>not</b> reuse the same test fixture for multiple tests. Any changes one test makes to the fixture do not affect other tests.</p>
<p>As an example, let's write tests for a FIFO queue class named <code><a class="el" href="class_queue.html">Queue</a></code>, which has the following interface:</p>
<div class="fragment"><div class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> E&gt;  <span class="comment">// E is the element type.</span></div>
<div class="line"><span class="keyword">class </span><a class="code hl_class" href="class_queue.html">Queue</a> {</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  <a class="code hl_class" href="class_queue.html">Queue</a>();</div>
<div class="line">  <span class="keywordtype">void</span> Enqueue(<span class="keyword">const</span> E&amp; element);</div>
<div class="line">  E* Dequeue();  <span class="comment">// Returns NULL if the queue is empty.</span></div>
<div class="line">  <span class="keywordtype">size_t</span> size() <span class="keyword">const</span>;</div>
<div class="line">  ...</div>
<div class="line">};</div>
<div class="ttc" id="aclass_queue_html"><div class="ttname"><a href="class_queue.html">Queue</a></div><div class="ttdef"><b>Definition</b> sample3-inl.h:72</div></div>
</div><!-- fragment --><p>First, define a fixture class. By convention, you should give it the name <code><a class="el" href="class_foo_test.html">FooTest</a></code> where <code>Foo</code> is the class being tested.</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>QueueTest : <span class="keyword">public</span> <a class="code hl_class" href="classtesting_1_1_test.html">::testing::Test</a> {</div>
<div class="line"> <span class="keyword">protected</span>:</div>
<div class="line">  <span class="keywordtype">void</span> SetUp()<span class="keyword"> override </span>{</div>
<div class="line">     q1_.Enqueue(1);</div>
<div class="line">     q2_.Enqueue(2);</div>
<div class="line">     q2_.Enqueue(3);</div>
<div class="line">  }</div>
<div class="line"> </div>
<div class="line">  <span class="comment">// void TearDown() override {}</span></div>
<div class="line"> </div>
<div class="line">  <a class="code hl_class" href="class_queue.html">Queue&lt;int&gt;</a> q0_;</div>
<div class="line">  <a class="code hl_class" href="class_queue.html">Queue&lt;int&gt;</a> q1_;</div>
<div class="line">  <a class="code hl_class" href="class_queue.html">Queue&lt;int&gt;</a> q2_;</div>
<div class="line">};</div>
<div class="ttc" id="aclasstesting_1_1_test_html"><div class="ttname"><a href="classtesting_1_1_test.html">testing::Test</a></div><div class="ttdef"><b>Definition</b> gtest.h:242</div></div>
</div><!-- fragment --><p>In this case, <code>TearDown()</code> is not needed since we don't have to clean up after each test, other than what's already done by the destructor.</p>
<p>Now we'll write tests using <code>TEST_F()</code> and this fixture.</p>
<div class="fragment"><div class="line">TEST_F(QueueTest, IsEmptyInitially) {</div>
<div class="line">  EXPECT_EQ(q0_.size(), 0);</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line">TEST_F(QueueTest, DequeueWorks) {</div>
<div class="line">  <span class="keywordtype">int</span>* n = q0_.Dequeue();</div>
<div class="line">  EXPECT_EQ(n, <span class="keyword">nullptr</span>);</div>
<div class="line"> </div>
<div class="line">  n = q1_.Dequeue();</div>
<div class="line">  ASSERT_NE(n, <span class="keyword">nullptr</span>);</div>
<div class="line">  EXPECT_EQ(*n, 1);</div>
<div class="line">  EXPECT_EQ(q1_.size(), 0);</div>
<div class="line">  <span class="keyword">delete</span> n;</div>
<div class="line"> </div>
<div class="line">  n = q2_.Dequeue();</div>
<div class="line">  ASSERT_NE(n, <span class="keyword">nullptr</span>);</div>
<div class="line">  EXPECT_EQ(*n, 2);</div>
<div class="line">  EXPECT_EQ(q2_.size(), 1);</div>
<div class="line">  <span class="keyword">delete</span> n;</div>
<div class="line">}</div>
</div><!-- fragment --><p>The above uses both <code>ASSERT_*</code> and <code>EXPECT_*</code> assertions. The rule of thumb is to use <code>EXPECT_*</code> when you want the test to continue to reveal more errors after the assertion failure, and use <code>ASSERT_*</code> when continuing after failure doesn't make sense. For example, the second assertion in the <code>Dequeue</code> test is <code>ASSERT_NE(n, nullptr)</code>, as we need to dereference the pointer <code>n</code> later, which would lead to a segfault when <code>n</code> is <code>NULL</code>.</p>
<p>When these tests run, the following happens:</p>
<ol type="1">
<li>googletest constructs a <code>QueueTest</code> object (let's call it <code>t1</code>).</li>
<li><code>t1.SetUp()</code> initializes <code>t1</code>.</li>
<li>The first test (<code>IsEmptyInitially</code>) runs on <code>t1</code>.</li>
<li><code>t1.TearDown()</code> cleans up after the test finishes.</li>
<li><code>t1</code> is destructed.</li>
<li>The above steps are repeated on another <code>QueueTest</code> object, this time running the <code>DequeueWorks</code> test.</li>
</ol>
<p><b>Availability</b>: Linux, Windows, Mac.</p>
<h1><a class="anchor" id="autotoc_md230"></a>
Invoking the Tests</h1>
<p><code>TEST()</code> and <code>TEST_F()</code> implicitly register their tests with googletest. So, unlike with many other C++ testing frameworks, you don't have to re-list all your defined tests in order to run them.</p>
<p>After defining your tests, you can run them with <code>RUN_ALL_TESTS()</code>, which returns <code>0</code> if all the tests are successful, or <code>1</code> otherwise. Note that <code>RUN_ALL_TESTS()</code> runs <em>all tests</em> in your link unit&ndash;they can be from different test suites, or even different source files.</p>
<p>When invoked, the <code>RUN_ALL_TESTS()</code> macro:</p>
<ul>
<li>Saves the state of all googletest flags.</li>
<li>Creates a test fixture object for the first test.</li>
<li>Initializes it via <code>SetUp()</code>.</li>
<li>Runs the test on the fixture object.</li>
<li>Cleans up the fixture via <code>TearDown()</code>.</li>
<li>Deletes the fixture.</li>
<li>Restores the state of all googletest flags.</li>
<li>Repeats the above steps for the next test, until all tests have run.</li>
</ul>
<p>If a fatal failure happens the subsequent steps will be skipped.</p>
<p>{: .callout .important} </p><blockquote class="doxtable">
<p>IMPORTANT: You must <b>not</b> ignore the return value of <code>RUN_ALL_TESTS()</code>, or you will get a compiler error. The rationale for this design is that the automated testing service determines whether a test has passed based on its exit code, not on its stdout/stderr output; thus your <code>main()</code> function must return the value of <code>RUN_ALL_TESTS()</code>.</p>
<p>Also, you should call <code>RUN_ALL_TESTS()</code> only <b>once</b>. Calling it more than once conflicts with some advanced googletest features (e.g., thread-safe <a href="advanced.md#death-tests">death tests</a>) and thus is not supported. </p>
</blockquote>
<p><b>Availability</b>: Linux, Windows, Mac.</p>
<h1><a class="anchor" id="autotoc_md231"></a>
Writing the main() Function</h1>
<p>Most users should <em>not</em> need to write their own <code>main</code> function and instead link with <code>gtest_main</code> (as opposed to with <code>gtest</code>), which defines a suitable entry point. See the end of this section for details. The remainder of this section should only apply when you need to do something custom before the tests run that cannot be expressed within the framework of fixtures and test suites.</p>
<p>If you write your own <code>main</code> function, it should return the value of <code>RUN_ALL_TESTS()</code>.</p>
<p>You can start from this boilerplate:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;this/package/foo.h&quot;</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#include &quot;gtest/gtest.h&quot;</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">namespace </span>my {</div>
<div class="line"><span class="keyword">namespace </span>project {</div>
<div class="line"><span class="keyword">namespace </span>{</div>
<div class="line"> </div>
<div class="line"><span class="comment">// The fixture for testing class Foo.</span></div>
<div class="line"><span class="keyword">class </span>FooTest : <span class="keyword">public</span> <a class="code hl_class" href="classtesting_1_1_test.html">::testing::Test</a> {</div>
<div class="line"> <span class="keyword">protected</span>:</div>
<div class="line">  <span class="comment">// You can remove any or all of the following functions if their bodies would</span></div>
<div class="line">  <span class="comment">// be empty.</span></div>
<div class="line"> </div>
<div class="line">  FooTest() {</div>
<div class="line">     <span class="comment">// You can do set-up work for each test here.</span></div>
<div class="line">  }</div>
<div class="line"> </div>
<div class="line">  ~FooTest()<span class="keyword"> override </span>{</div>
<div class="line">     <span class="comment">// You can do clean-up work that doesn&#39;t throw exceptions here.</span></div>
<div class="line">  }</div>
<div class="line"> </div>
<div class="line">  <span class="comment">// If the constructor and destructor are not enough for setting up</span></div>
<div class="line">  <span class="comment">// and cleaning up each test, you can define the following methods:</span></div>
<div class="line"> </div>
<div class="line">  <span class="keywordtype">void</span> SetUp()<span class="keyword"> override </span>{</div>
<div class="line">     <span class="comment">// Code here will be called immediately after the constructor (right</span></div>
<div class="line">     <span class="comment">// before each test).</span></div>
<div class="line">  }</div>
<div class="line"> </div>
<div class="line">  <span class="keywordtype">void</span> TearDown()<span class="keyword"> override </span>{</div>
<div class="line">     <span class="comment">// Code here will be called immediately after each test (right</span></div>
<div class="line">     <span class="comment">// before the destructor).</span></div>
<div class="line">  }</div>
<div class="line"> </div>
<div class="line">  <span class="comment">// Class members declared here can be used by all tests in the test suite</span></div>
<div class="line">  <span class="comment">// for Foo.</span></div>
<div class="line">};</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Tests that the Foo::Bar() method does Abc.</span></div>
<div class="line">TEST_F(FooTest, MethodBarDoesAbc) {</div>
<div class="line">  <span class="keyword">const</span> std::string input_filepath = <span class="stringliteral">&quot;this/package/testdata/myinputfile.dat&quot;</span>;</div>
<div class="line">  <span class="keyword">const</span> std::string output_filepath = <span class="stringliteral">&quot;this/package/testdata/myoutputfile.dat&quot;</span>;</div>
<div class="line">  Foo f;</div>
<div class="line">  EXPECT_EQ(f.Bar(input_filepath, output_filepath), 0);</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Tests that Foo does Xyz.</span></div>
<div class="line">TEST_F(FooTest, DoesXyz) {</div>
<div class="line">  <span class="comment">// Exercises the Xyz feature of Foo.</span></div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line">}  <span class="comment">// namespace</span></div>
<div class="line">}  <span class="comment">// namespace project</span></div>
<div class="line">}  <span class="comment">// namespace my</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv) {</div>
<div class="line">  ::testing::InitGoogleTest(&amp;argc, argv);</div>
<div class="line">  <span class="keywordflow">return</span> RUN_ALL_TESTS();</div>
<div class="line">}</div>
</div><!-- fragment --><p>The <code>testing::InitGoogleTest()</code> function parses the command line for googletest flags, and removes all recognized flags. This allows the user to control a test program's behavior via various flags, which we'll cover in the <a class="el" href="md__build_2__deps_2googletest-src_2docs_2advanced.html">AdvancedGuide</a>. You <b>must</b> call this function before calling <code>RUN_ALL_TESTS()</code>, or the flags won't be properly initialized.</p>
<p>On Windows, <code>InitGoogleTest()</code> also works with wide strings, so it can be used in programs compiled in <code>UNICODE</code> mode as well.</p>
<p>But maybe you think that writing all those <code>main</code> functions is too much work? We agree with you completely, and that's why Google Test provides a basic implementation of main(). If it fits your needs, then just link your test with the <code>gtest_main</code> library and you are good to go.</p>
<p>{: .callout .note} NOTE: <code>ParseGUnitFlags()</code> is deprecated in favor of <code>InitGoogleTest()</code>.</p>
<h1><a class="anchor" id="autotoc_md232"></a>
Known Limitations</h1>
<ul>
<li>Google Test is designed to be thread-safe. The implementation is thread-safe on systems where the <code>pthreads</code> library is available. It is currently <em>unsafe</em> to use Google Test assertions from two threads concurrently on other systems (e.g. Windows). In most tests this is not an issue as usually the assertions are done in the main thread. If you want to help, you can volunteer to implement the necessary synchronization primitives in <code>gtest-port.h</code> for your platform. </li>
</ul>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.13.2 </li>
  </ul>
</div>
</body>
</html>
